package com.veng.springbootdemo.dao;

import com.mongodb.client.result.UpdateResult;
import com.veng.springbootdemo.po.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

@Component
public class StudentUpdateRepositoryImp implements StudentUpdateRepository {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void updateStudent(Student student) {
        Query query=new Query(Criteria.where("NAME").is(student.getName()));
        Update update = new Update().set("DNAME", student.getDname()).set("SEX","女");
        //更新查询返回结果集的第一条
//        UpdateResult result =mongoTemplate.updateFirst(query, update, Student.class);
        //更新查询返回结果集的所有
        UpdateResult updateResult = mongoTemplate.updateMulti(query, update, Student.class);
//        if (updateResult != null) {
//            long matchedCount = updateResult.getMatchedCount();
//            System.out.println(matchedCount);
//        }

    }
}
